package com.example.demo.protocol;

import org.jetlinks.core.ProtocolSupport;
import org.jetlinks.core.defaults.CompositeProtocolSupport;
import org.jetlinks.core.message.codec.DefaultTransport;
import org.jetlinks.core.spi.ProtocolSupportProvider;
import org.jetlinks.core.spi.ServiceContext;
import org.jetlinks.supports.official.JetLinksDeviceMetadataCodec;
import reactor.core.publisher.Mono;

/**
 * 协议入口类
 */
public class DemoProtocolSupportProvider implements ProtocolSupportProvider {

    @Override
    public Mono<? extends ProtocolSupport> create(ServiceContext context) {
        CompositeProtocolSupport support = new CompositeProtocolSupport();
        // 协议ID
        support.setId("demo-v1");
        // 协议名称
        support.setName("演示协议v1");
        // 协议说明
        support.setDescription("演示协议");
        // 物模型编解码，固定为JetLinksDeviceMetadataCodec
        support.setMetadataCodec(new JetLinksDeviceMetadataCodec());
        //TCP消息编解码器
        DemoTcpMessageCodec codec = new DemoTcpMessageCodec();
        // 两个参数，协议支持和编解码类DemoDeviceMessageCodec中保持一致，第二个参数定义使用的编码解码类
        support.addMessageCodecSupport(DefaultTransport.TCP, () -> Mono.just(codec));
        return Mono.just(support);
    }
}
